#include "stdafx.h"
#include "RF60xDeviceNet.h"
#include "include\RF60xDevice.h"

using namespace RFDeviceNet;
using namespace RFDevice;
using namespace System;
using namespace System::Runtime::InteropServices;

RF60xDeviceNet^ RF60xDeviceNet::CreateRF60xDevice()
{
	RF60xDevice* device = new RF60xDevice();
	RF60xDeviceNet^ dev = gcnew RF60xDeviceNet(device);
	return dev;
}

//ctor
RF60xDeviceNet::RF60xDeviceNet(RFDevice::RF60xDevice* device) : RFComDeviceNet(device)
{
	_rf60xDevice = device;
}

//ctor
RF60xDeviceNet::RF60xDeviceNet()
{
	_rf60xDevice = new RF60xDevice();
}
//dtor
RF60xDeviceNet::~RF60xDeviceNet()
{
	_rf60xDevice->~RF60xDevice();
	_rf60xDevice = 0;
}

bool RF60xDeviceNet::OpenPort(String^ port_Name, DWORD speed)
{
	const char* str = (const char*)(void*)Marshal::StringToHGlobalAnsi(port_Name);
	return _rf60xDevice->OpenPort(str, speed);
}

void RF60xDeviceNet::BindNetworkAddress(BYTE address)
{
	_rf60xDevice->BindNetworkAddress(address);
}

bool RF60xDeviceNet::GetTimeouts(DWORD% readIntervalTimeout, DWORD% readTotalTimeoutMultiplier, 
							DWORD% readTotalTimeoutConstant, DWORD% writeTotalTimeoutMultiplier, 
							DWORD% writeTotalTimeoutConstant)
{
	LPDWORD preadIntervalTimeout = new DWORD;
	LPDWORD preadTotalTimeoutMultiplier = new DWORD;
	LPDWORD preadTotalTimeoutConstant = new DWORD;
	LPDWORD pwriteTotalTimeoutMultiplier = new DWORD;
	LPDWORD pwriteTotalTimeoutConstant = new DWORD;
	bool ret = _rf60xDevice->GetTimeouts(preadIntervalTimeout, preadTotalTimeoutMultiplier, preadTotalTimeoutConstant, pwriteTotalTimeoutMultiplier, pwriteTotalTimeoutConstant);
	readIntervalTimeout = *preadIntervalTimeout;
	readTotalTimeoutMultiplier = *preadTotalTimeoutMultiplier;
	readTotalTimeoutConstant = *preadTotalTimeoutConstant;
	writeTotalTimeoutMultiplier = *pwriteTotalTimeoutMultiplier;
	writeTotalTimeoutConstant = *pwriteTotalTimeoutConstant;
	return ret;
}

bool RF60xDeviceNet::SetTimeouts(DWORD readIntervalTimeout, DWORD readTotalTimeoutMultiplier, 
							DWORD readTotalTimeoutConstant, DWORD writeTotalTimeoutMultiplier, 
							DWORD writeTotalTimeoutConstant)
{
	bool ret = _rf60xDevice->SetTimeouts(readIntervalTimeout, readTotalTimeoutMultiplier, readTotalTimeoutConstant, writeTotalTimeoutMultiplier, writeTotalTimeoutConstant);
	return ret;
}

bool RF60xDeviceNet::HelloCmd(BYTE address, _RF60x_HELLO_ANSWER_Net^% helloAnswer)
{
	_RF60x_HELLO_ANSWER_* pa = new _RF60x_HELLO_ANSWER_();
	helloAnswer->GetOriginal(pa);
	bool toRet = _rf60xDevice->HelloCmd(address, pa);
	helloAnswer = gcnew _RF60x_HELLO_ANSWER_Net(pa);
	return toRet;
}

bool RF60xDeviceNet::HelloCmd()
{
	return _rf60xDevice->HelloCmd();
}

bool RF60xDeviceNet::GetStreamMeasures(_RF60x_STREAM_VALUE_Net^% streamValues, DWORD measuresCount, 
								  DWORD% readedMeasuresCount, DWORD% brokenPacketsCount)
{
	_RF60x_STREAM_VALUE_* pStruct = new _RF60x_STREAM_VALUE_();
	streamValues->GetOriginal(pStruct);
	DWORD* preadedMeasuresCount = new DWORD;
	DWORD* pbrokenPacketsCount = new DWORD;
	bool ret = _rf60xDevice->GetStreamMeasures(pStruct, measuresCount, preadedMeasuresCount, pbrokenPacketsCount);
	readedMeasuresCount = *preadedMeasuresCount;
	brokenPacketsCount = *pbrokenPacketsCount;
	streamValues = gcnew _RF60x_STREAM_VALUE_Net(pStruct);
	return ret;
}

float RF60xDeviceNet::GetSingleMeasure()
{
	return _rf60xDevice->GetSingleMeasure();
}

bool RF60xDeviceNet::EnableSensor(bool enable)
{
	return _rf60xDevice->EnableSensor(enable);
}

bool RF60xDeviceNet::EnableAnalogOut(bool enable)
{
	return _rf60xDevice->EnableAnalogOut(enable);
}

bool RF60xDeviceNet::SetSampleAveraging(BYTE value)
{
	return _rf60xDevice->SetSampleAveraging(value);
}

bool RF60xDeviceNet::SetAverageModeByQuantity()
{
	return _rf60xDevice->SetAverageModeByQuantity();
}

bool RF60xDeviceNet::SetAverageModeByTime()
{
	return _rf60xDevice->SetAverageModeByTime();
}

bool RF60xDeviceNet::SetCANModeByRequest()
{
	return _rf60xDevice->SetCANModeByRequest();
}

bool RF60xDeviceNet::SetCANModeByClk()
{
	return _rf60xDevice->SetCANModeByClk();
}

bool RF60xDeviceNet::SetLogicOutModeToRangeOverflow()
{
	return _rf60xDevice->SetLogicOutModeToRangeOverflow();
}

bool RF60xDeviceNet::SetLogicOutModeToMutualSynchronization()
{
	return _rf60xDevice->SetLogicOutModeToMutualSynchronization();
}

bool RF60xDeviceNet::SetLogicOutModeToResZeroing()
{
	return _rf60xDevice->SetLogicOutModeToResZeroing();
}

bool RF60xDeviceNet::SetLogicOutModeToLaserPower()
{
	return _rf60xDevice->SetLogicOutModeToLaserPower();
}

bool RF60xDeviceNet::SetAnalogOutModeToFrameMode()
{
	return _rf60xDevice->SetAnalogOutModeToFrameMode();
}